package com.englisheveryday.net;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.util.List;

import com.englisheveryday.utils.L;

/**
 * 下载器
 * @author cheney
 *
 */
public class Downloader {
	//完整的URL路径
	private String url;
	//完整的目标文件路径
	private String desFileName;
	//设置监听器
	private OnSocketTimeoutListener onSocketTimeoutListener;
	private OnFileNotExistListener onFileNotExistListener;
	private OnIOExceptionListener onIOExceptionListener;
	private OnCompleteListener onCompleteListener;
	
	public void download(){
		downloadFromUrl(url,desFileName);
	}
	
	//根据URL下载
	public void downloadFromUrl(String remoteUrl,String desFileName){
		HttpURLConnection connection = null;
		BufferedInputStream in = null;
		BufferedOutputStream out = null;
		try{
			L.d("url:"+remoteUrl);
			URL url = new URL(remoteUrl);
			connection = (HttpURLConnection)url.openConnection();
			connection.setConnectTimeout(10*1000);
			connection.setReadTimeout(10*1000);
			connection.connect();
			if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){
				in = new BufferedInputStream(connection.getInputStream());
				out = new BufferedOutputStream(new FileOutputStream(new File(desFileName)));
				byte[] buffer = new byte[1024];
				int length = 0;
				while((length=in.read(buffer))!=-1){
					out.write(buffer,0, length);
				}
				
				if(onCompleteListener!=null)
					onCompleteListener.onComplete(remoteUrl);
			}else {
				if(onFileNotExistListener!=null)
					onFileNotExistListener.onFileNotExist(remoteUrl);
			}
		}catch(SocketTimeoutException e){
			e.printStackTrace();
			if(onSocketTimeoutListener!=null)
				onSocketTimeoutListener.onSocketTimeout();
		}catch(IOException e){
			e.printStackTrace();
			if(onIOExceptionListener!=null)
				onIOExceptionListener.onIOException();
		}finally {
			if(connection!=null)
				connection.disconnect();
			if(out!=null){
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(in!=null){
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	/**
	 * 从URL 下载
	 * @param remoteUrl
	 * @param baseDir
	 * @param fileName
	 */
	public void downloadAllFromUrl(String remoteUrl,String baseDir,List<String> fileName){
		
	}
	
	/**
	 * 当超时時进行回调
	 * @author cheney
	 */
	public interface OnSocketTimeoutListener {
		public void onSocketTimeout();
	}
	
	/**
	 * 文件不存在
	 * @author cheney
	 */
	public interface OnFileNotExistListener {
		public void onFileNotExist(String fileName);
	}
	
	/**
	 * 传输错误
	 */
	public interface OnIOExceptionListener {
		public void onIOException();
	}

	/**
	 * 下载完成
	 * @return
	 */
	public interface OnCompleteListener {
		public void onComplete(String fileName);
	}
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getDesFileName() {
		return desFileName;
	}

	public void setDesFileName(String desFileName) {
		this.desFileName = desFileName;
	}

	public OnSocketTimeoutListener getOnSocketTimeoutListener() {
		return onSocketTimeoutListener;
	}

	public void setOnSocketTimeoutListener(
			OnSocketTimeoutListener onSocketTimeoutListener) {
		this.onSocketTimeoutListener = onSocketTimeoutListener;
	}

	public OnFileNotExistListener getOnFileNotExistListener() {
		return onFileNotExistListener;
	}

	public void setOnFileNotExistListener(
			OnFileNotExistListener onFileNotExistListener) {
		this.onFileNotExistListener = onFileNotExistListener;
	}

	public OnIOExceptionListener getOnIOExceptionListener() {
		return onIOExceptionListener;
	}

	public void setOnIOExceptionListener(OnIOExceptionListener onIOExceptionListener) {
		this.onIOExceptionListener = onIOExceptionListener;
	}

	public OnCompleteListener getOnCompleteListener() {
		return onCompleteListener;
	}

	public void setOnCompleteListener(OnCompleteListener onCompleteListener) {
		this.onCompleteListener = onCompleteListener;
	}
	
}
